package com.express.gateway.manage.options;

import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;

/**
 * 应用程序的端口号配置默认http=8330,https=8430,websocket=8530,
 *
 * @author flower
 */
public class ServerOptions extends HttpServerOptions {
    /**
     * 默认HTTP端口号
     */
    public final static int HTTP_DEFAULT_PORT = 8330;
    /**
     * 默认的HTTPS端口号
     */
    public final static int HTTPS_DEFAULT_PORT = 8430;
    /**
     * 默认的webSocket端口号
     */
    public final static int WEB_SOCKET_DEFAULT_PORT = 8530;
    private boolean createHttp = true;// 是否开启http服务
    private boolean createHttps = false;// 是否开启https服务
    private boolean createWebSocket = false;// 是否开启WebSocket服务
    private int httpPort = HTTP_DEFAULT_PORT;// HTTP服务器的端口号
    private int httpsPort = HTTPS_DEFAULT_PORT;// HTTPS服务器的端口号
    private int webSocketPort = WEB_SOCKET_DEFAULT_PORT;// WebSocket服务器的端口号
    private CertOptions certOptions;// 服务器证书
    private String custom;// 拓展配置

    /**
     * 创建一个默认端口号的端口配置
     */
    public ServerOptions() {
        super();
    }

    /**
     * 通过json实例化一个对象
     *
     * @param json
     */
    private ServerOptions(JsonObject json) {
        super(json);
    }

    /**
     * 将对象转换为json
     *
     * @return
     */
    public JsonObject toJson() {
        JsonObject json = new JsonObject();
        json.put("createHttp", this.createHttp);
        json.put("createHttps", this.createHttps);
        json.put("createwebSocket", this.createWebSocket);
        json.put("httpPort", this.httpPort);
        json.put("httpsPort", this.httpsPort);
        json.put("webSocketPort", this.webSocketPort);
        if (certOptions != null) {
            if (!certOptions.toJson().isEmpty()) {
                json.put("certOptions", certOptions.toJson());
            }
        }
        if (custom != null) {
            json.put("custom", this.custom);
        }
        return json;
    }

    /**
     * 将一个json对象装换为VxApiCertOptions,如果JsonObject为null或者或者JsonObject没有数据返回null
     *
     * @param obj
     * @return
     */
    public static ServerOptions fromJson(JsonObject obj) {
        ServerOptions options;
        if (obj.getValue("custom") instanceof String) {
            try {
                options = new ServerOptions(new JsonObject(obj.getString("custom")));
            } catch (Exception e) {
                options = new ServerOptions();
            }
            options.setCustom(obj.getString("custom"));
        } else {
            options = new ServerOptions();
        }
        if (obj.getValue("httpPort") instanceof Number) {
            options.setHttpPort(((Number) obj.getValue("httpPort")).intValue());
        }
        if (obj.getValue("httpsPort") instanceof Number) {
            options.setHttpsPort(((Number) obj.getValue("httpsPort")).intValue());
        }
        if (obj.getValue("webSocketPort") instanceof Number) {
            options.setWebSocketPort(((Number) obj.getValue("webSocketPort")).intValue());
        }
        if (obj.getValue("createHttp") instanceof Boolean) {
            options.setCreateHttp((Boolean) obj.getValue("createHttp"));
        }
        if (obj.getValue("createHttps") instanceof Boolean) {
            options.setCreateHttps((Boolean) obj.getValue("createHttps"));
        }
        if (obj.getValue("createwebSocket") instanceof Boolean) {
            options.setCreateWebSocket((Boolean) obj.getValue("createwebSocket"));
        }
        if (obj.getValue("certOptions") instanceof JsonObject) {
            options.setCertOptions(CertOptions.fromJson(obj.getJsonObject("certOptions")));
        }
        return options;
    }

    /**
     * 获得是否开启http服务器
     *
     * @return
     */
    public boolean isCreateHttp() {
        return createHttp;
    }

    /**
     * 设置是否开启http服务器
     *
     * @param createHttp
     */
    public void setCreateHttp(boolean createHttp) {
        this.createHttp = createHttp;
    }

    /**
     * 获得是否开启http服务器
     *
     * @return
     */
    public boolean isCreateHttps() {
        return createHttps;
    }

    /**
     * 设置是否开启https服务器
     *
     * @param createHttps
     */
    public void setCreateHttps(boolean createHttps) {
        this.createHttps = createHttps;
    }

    /**
     * 获得是否开启webSocket服务器
     *
     * @return
     */
    public boolean isCreateWebSocket() {
        return createWebSocket;
    }

    /**
     * 设置是否开启webSocket服务器
     *
     * @param createWebSocket
     */
    public void setCreateWebSocket(boolean createWebSocket) {
        this.createWebSocket = createWebSocket;
    }

    public int getHttpPort() {
        return httpPort;
    }

    public ServerOptions setHttpPort(int httpPort) {
        this.httpPort = httpPort;
        return this;
    }

    public int getHttpsPort() {
        return httpsPort;
    }

    public ServerOptions setHttpsPort(int httpsPort) {
        this.httpsPort = httpsPort;
        return this;
    }

    public int getWebSocketPort() {
        return webSocketPort;
    }

    public ServerOptions setWebSocketPort(int webSocketPort) {
        this.webSocketPort = webSocketPort;
        return this;
    }

    /**
     * 获得服务器证书
     *
     * @return
     */
    public CertOptions getCertOptions() {
        return certOptions;
    }

    /**
     * 设置服务器证书
     *
     * @param certOptions
     */
    public void setCertOptions(CertOptions certOptions) {
        this.certOptions = certOptions;
    }

    /**
     * 获得拓展配置信息
     *
     * @return
     */
    public String getCustom() {
        return custom;
    }

    /**
     * 设置拓展配置信息
     *
     * @param custom
     */
    public void setCustom(String custom) {
        this.custom = custom;
    }

    @Override
    public String toString() {
        return "ServerOptions [createHttp=" + createHttp + ", createHttps=" + createHttps + ", createwebSocket=" + createWebSocket + ", httpPort=" + httpPort + ", httpsPort=" + httpsPort + ", webSocketPort=" + webSocketPort + ", certOptions=" + certOptions + ", custom=" + custom + "]";
    }

}
